package ar.edu.uade.fain.ad.test.server.dao.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

import ar.edu.uade.fain.ad.test.server.entity.Contrato;
import ar.edu.uade.fain.ad.test.server.entity.Convenio;
import ar.edu.uade.fain.ad.test.server.entity.Coordinador;
import ar.edu.uade.fain.ad.test.server.entity.Desarrollador;
import ar.edu.uade.fain.ad.test.server.entity.Funcion;
import ar.edu.uade.fain.ad.test.server.entity.Integrante;
import ar.edu.uade.fain.ad.test.server.entity.PersistentObject;
import ar.edu.uade.fain.ad.test.server.entity.Proyecto;

public class HibernateUtil {

	private static HibernateUtil instance;

	private static final Class<?>[] annotatedClasses = {
		Contrato.class,
		Convenio.class,
		Coordinador.class,
		Desarrollador.class,
		Funcion.class,
		Integrante.class,
		PersistentObject.class,
		Proyecto.class
	};

	private SessionFactory sessionFactory;

	private HibernateUtil() {
		super();
		init();
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	private void init() {
		final AnnotationConfiguration configuration = 
				new AnnotationConfiguration();
		for (Class<?> clazz : getAnnotatedClasses()) {
			configuration.addAnnotatedClass(clazz);
		}
		setSessionFactory(configuration.buildSessionFactory());
	}

	public static HibernateUtil get() {
		if (instance == null) {
			instance = new HibernateUtil();
		}
		return instance;
	}

	protected Class<?>[] getAnnotatedClasses() {
		return annotatedClasses;
	}

	public Session openSession() {
		return getSessionFactory().openSession();
	}
	
}
